﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EIS_desktop
{
    public partial class BalanceSetUp : Form
    {
        private     System.Data.SqlClient.SqlConnection sqlcon; 
        private      System.Data.SqlClient.SqlCommand sqlcmd;
        private      System.Data.SqlClient.SqlDataAdapter sqladp;
        private bool cmbLoading;
        public BalanceSetUp()
        {
            InitializeComponent();

        }

        private void BalanceSetUp_Load(object sender, EventArgs e)
        {
            //this.MdiParent = MainForm.ActiveForm; ;
            sqlcon = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constr"]);
            sqlcmd = new System.Data.SqlClient.SqlCommand("Select EmpId,FirstName from Employee", sqlcon);
             sqladp = new System.Data.SqlClient.SqlDataAdapter(sqlcmd);
            DataTable dt = new DataTable();
            sqladp.Fill(dt);
                      
            cmbEmployee.DataSource = dt;
            cmbEmployee.DisplayMember = "FirstName";
            cmbEmployee.ValueMember = "EmpId";
            
            this.cmbEmployee.SelectedIndexChanged += new System.EventHandler(this.cmbEmployee_SelectedIndexChanged);
            cmbEmployee.SelectedIndex = -1;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            sqlcon = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constr"]);
            sqlcmd = new System.Data.SqlClient.SqlCommand("select count(*) from BalanceIntialize where EmpId = " + cmbEmployee.SelectedValue,sqlcon);
            sqlcon.Open();
            int count = (int)sqlcmd.ExecuteScalar();
            sqlcon.Close();

            if (count > 0)
            {
                sqlcmd = new System.Data.SqlClient.SqlCommand("update BalanceIntialize set IntialBalance = " + txtBalance.Text + " where EmpId = " + cmbEmployee.SelectedValue, sqlcon);
            }
            else
                sqlcmd = new System.Data.SqlClient.SqlCommand("INSERT INTO BalanceIntialize values (" + cmbEmployee.SelectedValue + "," + txtBalance.Text + ")", sqlcon);

            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
        }

        private void cmbEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            sqlcon = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constr"]);
            sqlcmd = new System.Data.SqlClient.SqlCommand("select intialbalance from BalanceIntialize where EmpId = " + cmbEmployee.SelectedValue, sqlcon);
            sqlcon.Open();
           if (balance != null)
               txtBalance.Text = balance.ToString();
           else
               txtBalance.Text = null;
            sqlcon.Close();
        }



        public object balance { get; set; }
    }
}
